﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySync2.Common.Interfaces;
using LibrarySync2.Business.FileSystem;

namespace LibrarySync2.ChildForms
{
    public partial class VerifyUpdateFilesForm : Form
    {
        List<MediaFileUpdateFormListItem> ListItems = new List<MediaFileUpdateFormListItem>();

        public IFileSystemUpdate FileSystemUpdate { get; protected set; }

        public VerifyUpdateFilesForm(IFileSystemUpdate fileSystemUpdate)
        {
            FileSystemUpdate = fileSystemUpdate;
            InitializeComponent();

            CreateListItems();
            BindListItems();
            CalculateSelectedPayloadSize();
        }

        private void CreateListItems()
        {
            foreach (var updateItem in FileSystemUpdate)
            {
                ListItems.Add(new MediaFileUpdateFormListItem(true, updateItem));
            }
        }

        private void BindListItems()
        {
            gvUpdateItems.DataSource = ListItems;
        }

        private void CalculateSelectedPayloadSize()
        {
            long calculatedSize = 0L;
            ListItems.FindAll(li => li.IsSelected).ForEach(li => calculatedSize += li.Size);
            lblPayloadSizeValue.Text = calculatedSize.ToString() + " MB";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var selectedItems = ListItems.FindAll(li => li.IsSelected);
            FileSystemUpdate.Clear();
            selectedItems.ForEach(si => FileSystemUpdate.Add(si.ItemUpdate));

            DialogResult = DialogResult.OK;
            Close();
        }

        private void gvUpdateItems_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            CalculateSelectedPayloadSize();
        }
    }
}
